
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgMatchRewardService;
import com.jf.cloud.service.sysmanager.entity.PubgClub;
import com.jf.cloud.service.sysmanager.entity.PubgMatchReward;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.dao.IPubgMatchService;
import com.jf.cloud.service.sysmanager.entity.PubgMatch;


/**
 * 员工服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgMatchService")
public class PubgMatchServiceImpl extends BaseService<PubgMatch, Long> implements IPubgMatchService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgMatchServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	private RowMapper<PubgMatch> rowMapper = new BeanPropertyRowMapper<PubgMatch>(GenericsUtils.getSuperClassGenricType(this.getClass()));

	@Resource
	private IPubgMatchRewardService pubgMatchRewardService;

	/**
	 * 根据主键查询员工
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public PubgMatch findPubgMatch(Long rowId) throws Exception {
		try {
			return this.baseDao.findObject("", rowMapper, rowId);
		} catch (Exception e) {
			throw new Exception("根据主键查询员工出错！", e);
		}
	}

	/**
	 * 根据查询条件获取员工列表
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public List<PubgMatch> findPubgMatchList(Condition qc) throws Exception {
		try {
			return this.baseDao.findList("", rowMapper);
		} catch (Exception e) {
			throw new Exception("根据查询条件获取员工列表出错！", e);
		}
	}

	/**
	 * 新增/修改员工
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public PubgMatch saveOrUpdatePubgMatch(PubgMatch pubgMatch) throws Exception {
		try {
			return this.baseDao.saveAll(pubgMatch);
		} catch (Exception e) {
			throw new Exception("新增/修改员工出错！", e);
		}
	}

	/**
	 * 添加赛事
	 * @param pubgMatch
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean saveMatch(PubgMatch pubgMatch, PubgUser pubgUser) throws Exception{
		boolean flag = false;
		try{
			pubgMatch.setMatchPeopleNum(0);
			pubgMatch.setApplyPeopleNum(0);
			pubgMatch.setApplySumFinal(0.0);
			pubgMatch.setApplySumTemp(0.0);
			pubgMatch.setTeamAward(0.0);
			pubgMatch.setUserAward(0.0);
			pubgMatch.setClubProfit(0.0);
			PubgMatch match = this.baseDao.saveAll(pubgMatch);
			if (!StringUtil.isEmpty(pubgMatch.getAwardConfig())) {
				String [] arr = pubgMatch.getAwardConfig().split(",");
				for (int i=0; i<arr.length; i++) {
					PubgMatchReward pubgMatchReward = new PubgMatchReward();
					pubgMatchReward.setMatchId(match.getRowId());
					pubgMatchReward.setMatchName(match.getMatchName());
					pubgMatchReward.setRanking(i+1);
					pubgMatchReward.setAwardValue(arr[i]);
					pubgMatchReward.setOptId(pubgUser.getRowId());
					pubgMatchReward.setOptAccount(pubgUser.getUserAccount());
					pubgMatchReward.setOptTime(DateUtil.getCurrentDateTime());
					this.baseDao.saveAll(pubgMatchReward);
				}
			}
			flag = true;
		}catch (Exception e){
			log.info("添加赛事失败");
			flag = false;
		}
		return flag;
	}
}
